/*
 *  **************************************************************
 *  Copyright. SOPHOS SYSTEMS. 2010 ALL RIGHTS RESERVED.
 *
 *  This software is only to be used for the purpose for which it
 *  has been provided. No part of it is to be reproduced,
 *  disassembled, transmitted, stored in a retrieval system or
 *  translated in any human or computer language in any way or
 *  for any other purposes whatsoever without the prior written
 *  consent of SOPHOS SYSTEMS. 2010.
 *  ***************************************************************
 */
package com.sophossystems.client.asmon.dao.spring;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.sophossystems.client.asmon.dao.EstadosDao;
import com.sophossystems.client.asmon.dto.EstadosDTO;
import com.sophossystems.client.asmon.dto.SexoDTO;
import com.sophossystems.client.asmon.exceptions.DaoException;

/**
 * @author juanvelezballesteros
 *
 */
public class EstadosDaoImpl extends AbstractDAO implements EstadosDao {
	private static Logger logger = Logger.getLogger(EstadosDaoImpl.class);

	
		@Override
		public EstadosDTO findByPrimaryKey(final int idEstado) throws DaoException {
			EstadosDTO sexo = null;
			try {
				sexo = (EstadosDTO) getHibernateTemplate().execute(
						new HibernateCallback() {
							
							@Override
							public Object doInHibernate(Session session) throws HibernateException,
									SQLException {
								final Query oQuery = session.createQuery("SELECT s.idEstado as idEstado, " +
										"s.nombreEstado as nombreEstado FROM " 
										+ getTableName() + " s WHERE s.idEstado = :id");
								oQuery.setInteger("id", idEstado);
								EstadosDTO sexoDto = (EstadosDTO) 
										oQuery.setResultTransformer(
										Transformers.aliasToBean(
												EstadosDTO.class)).list().get(0);
								return sexoDto;
								
							}
						});
			} catch (Exception e) {
				logger.error("Error: getIdEstado, cause: " + e.getMessage(), e);
				throw new DaoException("Error: estado, cause: "
						+ e.getMessage());
			}
			return sexo;
		}

		@SuppressWarnings("unchecked")
		@Override
		public List<EstadosDTO> findAll() throws DaoException {

			List<EstadosDTO> estado = null;
			try {
				estado = (List<EstadosDTO>) getHibernateTemplate().execute(
						new HibernateCallback() {
							
							@Override
							public Object doInHibernate(Session session) throws HibernateException,
									SQLException {
								final Query oQuery = session.createQuery("SELECT s.idEstado as idEstado, " +
										"s.nombreEstado as nombreEstado FROM " 
										+ getTableName() + " s ORDER BY s.idEstado");
								List<SexoDTO> sexoDto = oQuery.setResultTransformer(
										Transformers.aliasToBean(EstadosDTO.class)).list();
								return sexoDto;
							}
						});
			} catch (Exception e) {
				logger.error("Error:estado, cause: " + e.getMessage(), e);
				throw new DaoException("Error: getSexo, cause: "
						+ e.getMessage());
			}
			return estado;
		}
		
		public String getTableName() {
			return "Estados";
		} 
	
}
